package com.wuliu.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuliu.common.core.domain.PageResult;
import com.wuliu.common.core.domain.entity.SysAppletUser;
import com.wuliu.common.utils.DateUtils;

import javax.annotation.Resource;

import com.wuliu.common.utils.SecurityUtils;
import com.wuliu.system.domain.dto.app.BasAddressDto;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.BasAddressMapper;
import com.wuliu.system.domain.BasAddress;
import com.wuliu.system.service.IBasAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 用户地址库Service业务层处理
 *
 * @author realue
 * @date 2023-10-28
 */
@Service
public class BasAddressServiceImpl extends ServiceImpl<BasAddressMapper, BasAddress> implements IBasAddressService {
    @Resource
    private BasAddressMapper basAddressMapper;

    /**
     * 查询用户地址库
     *
     * @param id 用户地址库主键
     * @return 用户地址库
     */
    @Override
    public BasAddress selectBasAddressById(String id) {
        return basAddressMapper.selectBasAddressById(id);
    }

    /**
     * 查询用户地址库列表
     *
     * @param basAddress 用户地址库
     * @return 用户地址库
     */
    @Override
    public List<BasAddress> selectBasAddressList(BasAddress basAddress) {
        return basAddressMapper.selectBasAddressList(basAddress);
    }

    /**
     * 新增用户地址库
     *
     * @param basAddress 用户地址库
     * @return 结果
     */
    @Override
    public int insertBasAddress(BasAddress basAddress) {
        basAddress.setCreateTime(DateUtils.getNowDate());
        return basAddressMapper.insertBasAddress(basAddress);
    }

    /**
     * 修改用户地址库
     *
     * @param basAddress 用户地址库
     * @return 结果
     */
    @Override
    public int updateBasAddress(BasAddress basAddress) {
        basAddress.setUpdateTime(DateUtils.getNowDate());
        return basAddressMapper.updateBasAddress(basAddress);
    }

    /**
     * 批量删除用户地址库
     *
     * @param ids 需要删除的用户地址库主键
     * @return 结果
     */
    @Override
    public int deleteBasAddressByIds(String[] ids) {
        return basAddressMapper.deleteBasAddressByIds(ids);
    }

    /**
     * 删除用户地址库信息
     *
     * @param id 用户地址库主键
     * @return 结果
     */
    @Override
    public int deleteBasAddressById(String id) {
        return basAddressMapper.deleteBasAddressById(id);
    }

    /**
     * 前端查询用户地址库
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult<BasAddress> selectList(BasAddressDto dto) {
        SysAppletUser user = SecurityUtils.getAppletUser();
        Page<BasAddress> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        basAddressMapper.selectPage(page, new LambdaQueryWrapper<BasAddress>()
                .eq(BasAddress::getUserId, user.getUserId())
                .eq(BasAddress::getSysAccount, user.getSysAccount())
                .eq(BasAddress::getType, dto.getType())
                .orderByDesc(BasAddress::getDefaultFlag));
        return new PageResult<>(page);
    }

    /**
     * 设为默认地址
     *
     * @param id
     * @return
     */
    @Override
    public Boolean setDefault(String id) {
        BasAddress basAddress = basAddressMapper.selectOne(new LambdaQueryWrapper<BasAddress>().eq(BasAddress::getId, id));
        BasAddress upAddress = new BasAddress();
        upAddress.setDefaultFlag(0);
        basAddressMapper.update(upAddress, new LambdaQueryWrapper<BasAddress>()
                .eq(BasAddress::getUserId, basAddress.getUserId())
                .eq(BasAddress::getType, basAddress.getType()));
        upAddress.setDefaultFlag(1);
        return basAddressMapper.update(upAddress, new LambdaQueryWrapper<BasAddress>().eq(BasAddress::getId, id)) > 0;
    }
}
